﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Adex;
using System.IO;

namespace MediaMonitor.Admin
{

    public partial class Media_Mgmt_List : Adex.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void radGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            DataTable dt = VProdVersion.Query().SetSelectList("*, CONVERT(VARCHAR, DateModified, 101) AS LastModifiedDate ").WHERE("FullVideo", true).OR("ShortVideo", true).ORDER_BY("DateModified DESC").ExecuteDataSet().Tables[0];
            radGrid.DataSource = dt;
        }


        protected void DelShortVideo(object sender, EventArgs e)
        {
            string pkID = ((LinkButton)(sender)).CommandArgument;

            if (pkID.Length > 0)
            {

                VProdVersion pv = new VProdVersion("ProdVersionCode", pkID);

                try
                {

                    //del physical files
                    if (File.Exists(Server.MapPath("../videos/" + pv.ProductCode) + "/" + pv.ShortVideoFile))
                        File.Delete(Server.MapPath("../videos/" + pv.ProductCode) + "/" + pv.ShortVideoFile);

                    pv.ShortVideo = false;
                    pv.ShortVideoFile = "";

                    pv.Save();
                    radGrid.Rebind();

                    radPanel.Alert("selected file deleted successfully");
                }

                catch (Exception ex)
                {
                    radPanel.Alert("Could not delete the selected file. [" + ex.Message + "]");

                }

            }
        }


        protected void DelFullVideo(object sender, EventArgs e)
        {
            string pkID = ((LinkButton)(sender)).CommandArgument;

            if (pkID.Length > 0)
            {

                ProdVersion pv = new ProdVersion(pkID);

                try
                {

                    //del physical files
                    if (File.Exists(Server.MapPath("../videos/" + pv.ProductCode) + "/" + pv.FullVideoFile))
                        File.Delete(Server.MapPath("../videos/" + pv.ProductCode) + "/" + pv.FullVideoFile);

                    pv.FullVideo = false;
                    pv.FullVideoFile = "";

                    pv.Save();
                    radGrid.Rebind();

                    radPanel.Alert("selected file deleted successfully");

                }

                catch (Exception ex)
                {
                    radPanel.Alert("Could not delete the selected file. [" + ex.Message + "]");


                }

            }
        }
    }
}